package com.weixing.mall.provider.controller.cms;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.weixing.mall.base.wrapper.Result;
import com.weixing.mall.base.wrapper.ResultUtil;
import com.weixing.mall.base.enums.WhetherEnum;
import com.weixing.mall.core.support.BaseController;
import com.weixing.mall.provider.model.domain.SubSite;
import com.weixing.mall.provider.model.dto.cms.SubSiteChgEnableDto;
import com.weixing.mall.provider.model.dto.cms.SubSiteDto;
import com.weixing.mall.provider.model.enums.cms.SubSiteEnum;
import com.weixing.mall.provider.service.ISubSiteService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * @Author wx
 * @Date 2019/7/25 16:52
 * @Version 1.0
 */
@RestController
@RequestMapping("subSite")
@Api(value = "子站API",tags = "子站")
public class SubSiteController extends BaseController {

    @Autowired
    private ISubSiteService subSiteService;

    @ApiOperation(value = "加载子站")
    @GetMapping("")
    public Result list(){

        return ResultUtil.success(subSiteService.selectOfPage(initMpPage()));
    }

    @ApiOperation(value = "加载子站列表1代表正价，2代表活动")
    @GetMapping("/byType/{type}")
    public Result byTypeList(@PathVariable int type){
        return ResultUtil.success(subSiteService.list(new QueryWrapper<SubSite>().eq("site_type",type).eq("is_enable",WhetherEnum.YES.getValue())));
    }
    @ApiOperation(value = "可用子站列表")
    @GetMapping("/enableList")
    public Result<List<SubSite>> channelList() {
        QueryWrapper<SubSite> wrapper = new QueryWrapper<>();
        wrapper.eq("is_enable", WhetherEnum.YES.getValue()).orderByAsc("order_by");
        List<SubSite> list = subSiteService.list(wrapper);
        return ResultUtil.success(list);
    }

    @ApiOperation(value = "加载活动子站列表,用于新增活动时候选择子站")
    @GetMapping("/act")
    public Result<List<SubSite>> actList() {
        QueryWrapper<SubSite> wrapper = new QueryWrapper<>();
        wrapper.eq("is_enable", WhetherEnum.YES.getValue()).eq("site_type", SubSiteEnum.ACTIVITY.getValue()).orderByAsc("order_by");
        List<SubSite> list = subSiteService.list(wrapper);
        return ResultUtil.success(list);
    }
    @ApiOperation(value = "新增子站")
    @PostMapping("save")
    public Result save(@Validated SubSiteDto subSiteDto){

        subSiteService.save(subSiteDto);
        return ResultUtil.success();
    }

    @ApiOperation(value = "删除子站")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "子站id",paramType = "path",dataType = "long")
    })
    @PostMapping("del/{id}")
    public  Result del(@PathVariable long id){
        return subSiteService.removeById(id)? ResultUtil.success():ResultUtil.error();
    }

    @ApiOperation(value = "批量删除子站")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "子站id",paramType = "path",dataType = "long")
    })
    @PostMapping("delAll/{ids}")
    public Result delAll(@PathVariable Long ids[]){
        List<Long> longs = Arrays.asList(ids);
        subSiteService.removeByIds(longs);
        return ResultUtil.success();
    }

    @ApiOperation(value = "修改状态")
    @PostMapping("chgEnable")
    public Result changeEnable(SubSiteChgEnableDto subSiteChgEnableDto){
        subSiteService.changeEnable(subSiteChgEnableDto);
        return ResultUtil.success();
    }

}
